package com.junxonline.instant.terminal.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.gson.reflect.TypeToken;
import com.junxonline.instant.common.constant.RedisConstant;
import com.junxonline.instant.common.constant.TerminalCommandMqConstant;
import com.junxonline.instant.common.constant.TerminalInfoConstant;
import com.junxonline.instant.common.util.common.RedisUtils;
import com.junxonline.instant.dao.entity.model.terminal.*;
import com.junxonline.instant.dao.mapper.device.TerminalMapper;
import com.junxonline.instant.dao.mapper.terminal.*;
import com.junxonline.instant.terminal.config.TerminalConfig;
import lombok.extern.log4j.Log4j2;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

/**
 * RocketMq拉取指令响应消费者
 *
 * @author JunX
 * @date 2021-12-02
 */
@Log4j2
@Component
@Transactional
@RocketMQMessageListener(consumerGroup = TerminalCommandMqConstant.GROUP_QUERY_COMMAND_RESPONSE, topic = TerminalCommandMqConstant.TOPIC_QUERY_COMMAND_RESPONSE)
public class TerminalQueryCommandResponseListener implements RocketMQListener<MessageExt> {

    @Autowired
    private TerminalConfig terminalConfig;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private TerminalMapper terminalMapper;

    @Autowired
    private TerminalVolumeMapper terminalVolumeMapper;

    @Autowired
    private TerminalDisplayTypeMapper terminalDisplayTypeMapper;

    /**
     * 消息解析与分发
     *
     * @param message 消息内容
     */
    @Override
    public void onMessage(MessageExt message) {
        String topic = message.getTopic();
        String tags = message.getTags();
        String body = new String(message.getBody());
        // 解析
        HashMap<String, Object> data = JSON.parseObject(body, new TypeToken<HashMap<String, Object>>() {}.getType());
        log.info("TerminalCommandResponseListener接收到消息 -> topic={}, tags={}, 消息内容={}", topic, tags, body);
        // 消息根据Topic与Tag进行分发处理
        if (topic.equals(TerminalCommandMqConstant.TOPIC_QUERY_COMMAND_RESPONSE)) {
            switch (tags) {
                case TerminalCommandMqConstant.TAG_QUERY_VOLUME_RESPONSE:
                    queryVolume(data);
                    break;
                case TerminalCommandMqConstant.TAG_QUERY_DISPLAY_TYPE_RESPONSE:
                    queryDisplayType(data);
                    break;
                case TerminalCommandMqConstant.TAG_PULL_SCREENSHOT_RESPONSE:
                    pullScreenshot(data);
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 拉取音量指令
     *
     * @param data 消息数据
     */
    public void queryVolume(HashMap<String, Object> data) {
        if (data != null) {
            String terminalCode = (String) data.get("terminalCode");
            Integer volume = (Integer) data.get("volume");
            // 先删除旧数据再插入新的数据
            terminalVolumeMapper.delete(new LambdaUpdateWrapper<TerminalVolume>().eq(TerminalVolume::getTerminalCode, terminalCode));
            TerminalVolume terminalVolume = new TerminalVolume();
            terminalVolume.setTerminalCode(terminalCode);
            terminalVolume.setVolume(volume);
            terminalVolumeMapper.insert(terminalVolume);
            // 存入缓存中并设定有效期
            redisUtils.hset(RedisConstant.KEY_TERMINAL_VOLUME + terminalCode, TerminalInfoConstant.KEY_VOLUME, volume, terminalConfig.getCommandExpireTime());
        }
    }

    /**
     * 拉取显示方式指令
     *
     * @param data 消息数据
     */
    public void queryDisplayType(HashMap<String, Object> data) {
        if (data != null) {
            String terminalCode = (String) data.get("terminalCode");
            Integer displayType = (Integer) data.get("displayType");
            // 先删除旧数据再插入新的数据
            terminalDisplayTypeMapper.delete(new LambdaUpdateWrapper<TerminalDisplayType>().eq(TerminalDisplayType::getTerminalCode, terminalCode));
            TerminalDisplayType terminalDisplayType = new TerminalDisplayType();
            terminalDisplayType.setTerminalCode(terminalCode);
            terminalDisplayType.setDisplayType(displayType);
            terminalDisplayTypeMapper.insert(terminalDisplayType);
            // 存入缓存中并设定有效期
            redisUtils.hset(RedisConstant.KEY_TERMINAL_DISPLAY_TYPE + terminalCode, TerminalInfoConstant.KEY_DISPLAY_TYPE, displayType, terminalConfig.getCommandExpireTime());
        }
    }

    /**
     * 拉取截屏指令
     *
     * @param data 消息数据
     */
    public void pullScreenshot(HashMap<String, Object> data) {
        if (data != null) {
            String terminalCode = (String) data.get("terminalCode");
            String url = (String) data.get("url");
            log.info("拉取到终端[" + terminalCode + "]截屏文件URL  => " + url);
            // 存入缓存中并设定有效期
            redisUtils.hset(RedisConstant.KEY_TERMINAL_SCREENSHOT + terminalCode, TerminalInfoConstant.KEY_SCREENSHOT, url, terminalConfig.getCommandExpireTime());
        }
    }

}
